package com.eye.catcher.controller;


import com.eye.catcher.param.CommonResponse;
import com.eye.catcher.constants.SbCode;
import com.eye.catcher.entity.TSysDaRoleModule;
import com.eye.catcher.param.ResponseData;
import com.eye.catcher.param.ResponseUtil;
import com.eye.catcher.rabbit.MQDto;
import com.eye.catcher.service.generate.ITSysDaRoleModuleService;
import com.eye.catcher.system.roleModule.param.RoleModuleDto;
import com.eye.catcher.system.roleModule.param.RoleModulePage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huzhiwei
 * @since 2022-02-08
 */
@Slf4j
@Controller
@RequestMapping("/eyeCatcher-system/sys-role-module")
public class TSysDaRoleModuleController extends TSysDaBaseController {
    private final static String MODULE_CODE = "/sys-role-module";
    private final static String MODULE_NAME = "角色菜单关联权限";

    @Autowired
    private ITSysDaRoleModuleService roleModuleService;

    @PostMapping("/addRoleModule")
    @ResponseBody
    public ResponseData addRoleModule(@RequestBody TSysDaRoleModule roleModule) {
        String methodCode = "/addRoleModule";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_ADD,roleModule.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            this.roleModuleService.addRoleModule(roleModule);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

    @PostMapping("/addBatchRoleModule")
    @ResponseBody
    public ResponseData addBatchRoleModule(@RequestBody TSysDaRoleModule roleModule) {
        String methodCode = "/addBatchRoleModule";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_ADD,roleModule.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            this.roleModuleService.addBatchRoleModule(roleModule);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }
    @PostMapping("/deleteRoleModuleByIds")
    @ResponseBody
    public ResponseData deleteRoleModuleByIds(@RequestParam(value = "roleModuleIds") String roleModuleIds) {
        String methodCode = "/deleteRoleModuleByIds";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_EDIT,"ids："+roleModuleIds,null,null);
            this.sendInitRabbitMqLog(mqDto);
            this.roleModuleService.deleteRoleModuleByIds(roleModuleIds);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

    @PostMapping("/deleteRoleModuleByModuleIds")
    @ResponseBody
    public ResponseData deleteRoleModuleByModuleIds(@RequestParam(value = "moduleIds") String moduleIds) {
        String methodCode = "/deleteRoleModuleByModuleIds";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_EDIT,"ids："+moduleIds,null,null);
            this.sendInitRabbitMqLog(mqDto);
            this.roleModuleService.deleteRoleModuleByModuleIds(moduleIds);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }


    @PostMapping("/deleteRoleModuleByRoleIds")
    @ResponseBody
    public ResponseData deleteRoleModuleByRoleIds(@RequestParam(value = "roleIds") String roleIds) {
        String methodCode = "/deleteRoleModuleByRoleIds";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_EDIT,"ids："+roleIds,null,null);
            this.sendInitRabbitMqLog(mqDto);
            this.roleModuleService.deleteRoleModuleByRoleIds(roleIds);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }


    /**
     * @author huzhiwei
     * @since 2022-02-08
     * @param roleModule
     * @return
     */
    @PostMapping("/getRoleModuleList")
    @ResponseBody
    public ResponseData getRoleModuleList(@RequestBody RoleModuleDto roleModule) {
        String methodCode = "/getRoleModuleList";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,
                    getClass(),SbCode.OPERATION_QUERY,roleModule.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            commonResponse = CommonResponse.success();
            commonResponse.setData(
                    this.roleModuleService.getPageRoleModuleList(
                            RoleModulePage.builder()
                                    .roleModule(roleModule)
                                    .page(this.getPageInfo(roleModule))
                                    .build()
                    )
            );
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }



}
